/**
 * Copyright (c) 2009-2011, chunquedong(YangJiandong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 
 * History:
 *     2010-05-05  Jed Young  Creation
 */
package chunmap.service.wms;

import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import chunmap.app.tools.SelectIterator;
import chunmap.data.feature.Feature;
import chunmap.data.feature.FeatureUtil;
import chunmap.model.coord.Transform;
import chunmap.model.elem.Envelope;
import chunmap.service.Service;
import chunmap.service.Util;
import chunmap.util.Logger;
import chunmap.view.Map;
import chunmap.view.layer.Layer;
import chunmap.view.layer.VectorLayer;

public class GetFeature implements Service{
	private static final Logger Log = new Logger(Logger.Debug,GetFeature.class.getName());
	protected String layer;
	
	public Envelope envelop;
	public Capabilities capbilities;
	public int height;
	public int width;
	public String formatName = "png";

	@Override
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String requestStr = request.getParameter("REQUEST");
		layer = request.getParameter("QUERY_LAYER");

		PrintStream out = new PrintStream(response.getOutputStream());
		if (requestStr.equalsIgnoreCase("GetFeature") || requestStr.contains("Feature")) {

			String xs = request.getParameter("X");
			String ys = request.getParameter("Y");
			//Envelope envelop = parseEnvelop(x,y);
			
			Map map = new Map(width, height);
			map.getView().setViewEnvelop(envelop);
			
			double x = Double.valueOf(xs);
			double y = Double.valueOf(ys);
			double d = 5d;
			double minx = x - d;
			double miny = y - d;
			double maxx = x + d;
			double maxy = y + d;
			Envelope env = new Envelope(minx, miny, maxx, maxy);
			Transform trans = map.getView().screen2World();
			Envelope env2 = env.transform(trans);
			
			String sizes= request.getParameter("FEATURE_COUNT");
			int size=100;
			if(sizes!=null){
				size=Integer.valueOf(sizes);
			}
			
			String s = select(env2,size);
			out.write(s.getBytes(Charset.forName("UTF-8")));
			
		} else {
			Log.log(Logger.Debug, "不支持的请求" + requestStr);
		}
	}

	private String select(Envelope envelop,int size) {

		SelectIterator it = new SelectIterator(envelop);
		List<Layer> layers = getLayers();
		//Log.log(Logger.Debug, "layer size " + layers.size());
		for (Layer layer : layers) {
			if (layer instanceof VectorLayer) {
				VectorLayer vl = (VectorLayer) layer;
				if (vl.isSelectabel()){
					vl.getFeatures().select(envelop, it);
				}
			}
		}
		//Log.log(Logger.Debug, "selected feature size " + it.features.size());
		List<Feature> fs=it.features;
		if(fs.size()>size){
			fs=fs.subList(0, size);
		}
		String s=FeatureUtil.LayerHeadString(it.features,envelop).toString();
		//Log.log(Logger.Debug, s);
		return s;
	}

	private List<Layer> getLayers() {
		if(layer==null || layer.equals("")){
			return capbilities.getLayers();
		}else{
			return Util.getLayersByName(capbilities.getLayers(),layer);
		}
	}
}